/*
 * Tencent is pleased to support the open source community by making TBase available.  
 * 
 * Copyright (C) 2019 Tencent.  All rights reserved.
 * 
 * TBase is licensed under the BSD 3-Clause License, except for the third-party component listed below. 
 * 
 * A copy of the BSD 3-Clause License is included in this file.
 * 
 * Other dependencies and licenses:
 * 
 * Open Source Software Licensed Under the PostgreSQL License: 
 * --------------------------------------------------------------------
 * 1. Postgres-XL XL9_5_STABLE
 * Portions Copyright (c) 2015-2016, 2ndQuadrant Ltd
 * Portions Copyright (c) 2012-2015, TransLattice, Inc.
 * Portions Copyright (c) 2010-2017, Postgres-XC Development Group
 * Portions Copyright (c) 1996-2015, The PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, The Regents of the University of California
 * 
 * Terms of the PostgreSQL License: 
 * --------------------------------------------------------------------
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without a written agreement
 * is hereby granted, provided that the above copyright notice and this
 * paragraph and the following two paragraphs appear in all copies.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 * 
 * 
 * Terms of the BSD 3-Clause License:
 * --------------------------------------------------------------------
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of Tencent nor the names of its contributors may be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 */
/*-------------------------------------------------------------------------
 *
 * toasting.c
 *      This file contains routines to support creation of toast tables
 *
 *
 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *      src/backend/catalog/toasting.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "access/tuptoaster.h"
#include "access/xact.h"
#include "catalog/binary_upgrade.h"
#include "catalog/dependency.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_type.h"
#include "catalog/toasting.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "storage/lock.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/syscache.h"

/* Potentially set by pg_upgrade_support functions */
Oid            binary_upgrade_next_toast_pg_type_oid = InvalidOid;

static void CheckAndCreateToastTable(Oid relOid, Datum reloptions,
                         LOCKMODE lockmode, bool check);
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
                   Datum reloptions, LOCKMODE lockmode, bool check);
static bool needs_toast_table(Relation rel);


/*
 * CreateToastTable variants
 *        If the table needs a toast table, and doesn't already have one,
 *        then create a toast table for it.
 *
 * reloptions for the toast table can be passed, too.  Pass (Datum) 0
 * for default reloptions.
 *
 * We expect the caller to have verified that the relation is a table and have
 * already done any necessary permission checks.  Callers expect this function
 * to end with CommandCounterIncrement if it makes any changes.
 */
void
AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
{
    CheckAndCreateToastTable(relOid, reloptions, lockmode, true);
}

void
NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
{
    CheckAndCreateToastTable(relOid, reloptions, lockmode, false);
}

void
NewRelationCreateToastTable(Oid relOid, Datum reloptions)
{
    CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false);
}

static void
CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check)
{
    Relation    rel;

    rel = heap_open(relOid, lockmode);

    /* create_toast_table does all the work */
    (void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode, check);

    heap_close(rel, NoLock);
}

/*
 * Create a toast table during bootstrap
 *
 * Here we need to prespecify the OIDs of the toast table and its index
 */
void
BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
{
    Relation    rel;

    rel = heap_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);

    if (rel->rd_rel->relkind != RELKIND_RELATION &&
        rel->rd_rel->relkind != RELKIND_MATVIEW)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("\"%s\" is not a table or materialized view",
                        relName)));

    /* create_toast_table does all the work */
    if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
                            AccessExclusiveLock, false))
        elog(ERROR, "\"%s\" does not require a toast table",
             relName);

    heap_close(rel, NoLock);
}


/*
 * create_toast_table --- internal workhorse
 *
 * rel is already opened and locked
 * toastOid and toastIndexOid are normally InvalidOid, but during
 * bootstrap they can be nonzero to specify hand-assigned OIDs
 */
static bool
create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
                   Datum reloptions, LOCKMODE lockmode, bool check)
{// #lizard forgives
    Oid            relOid = RelationGetRelid(rel);
    HeapTuple    reltup;
    TupleDesc    tupdesc;
    bool        shared_relation;
    bool        mapped_relation;
    Relation    toast_rel;
    Relation    class_rel;
    Oid            toast_relid;
    Oid            toast_typid = InvalidOid;
    Oid            namespaceid;
    char        toast_relname[NAMEDATALEN];
    char        toast_idxname[NAMEDATALEN];
    IndexInfo  *indexInfo;
    Oid            collationObjectId[2];
    Oid            classObjectId[2];
    int16        coloptions[2];
    ObjectAddress baseobject,
                toastobject;

    /*
     * Toast table is shared if and only if its parent is.
     *
     * We cannot allow toasting a shared relation after initdb (because
     * there's no way to mark it toasted in other databases' pg_class).
     */
    shared_relation = rel->rd_rel->relisshared;
    if (shared_relation && !IsBootstrapProcessingMode())
        ereport(ERROR,
                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                 errmsg("shared tables cannot be toasted after initdb")));

    /* It's mapped if and only if its parent is, too */
    mapped_relation = RelationIsMapped(rel);

    /*
     * Is it already toasted?
     */
    if (rel->rd_rel->reltoastrelid != InvalidOid)
        return false;

    /*
     * Check to see whether the table actually needs a TOAST table.
     */
    if (!IsBinaryUpgrade)
    {
        /* Normal mode, normal check */
        if (!needs_toast_table(rel))
            return false;
    }
    else
    {
        /*
         * In binary-upgrade mode, create a TOAST table if and only if
         * pg_upgrade told us to (ie, a TOAST table OID has been provided).
         *
         * This indicates that the old cluster had a TOAST table for the
         * current table.  We must create a TOAST table to receive the old
         * TOAST file, even if the table seems not to need one.
         *
         * Contrariwise, if the old cluster did not have a TOAST table, we
         * should be able to get along without one even if the new version's
         * needs_toast_table rules suggest we should have one.  There is a lot
         * of daylight between where we will create a TOAST table and where
         * one is really necessary to avoid failures, so small cross-version
         * differences in the when-to-create heuristic shouldn't be a problem.
         * If we tried to create a TOAST table anyway, we would have the
         * problem that it might take up an OID that will conflict with some
         * old-cluster table we haven't seen yet.
         */
        if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid) ||
            !OidIsValid(binary_upgrade_next_toast_pg_type_oid))
            return false;
    }

    /*
     * If requested check lockmode is sufficient. This is a cross check in
     * case of errors or conflicting decisions in earlier code.
     */
    if (check && lockmode != AccessExclusiveLock)
        elog(ERROR, "AccessExclusiveLock required to add toast table.");

    /*
     * Create the toast table and its index
     */
    snprintf(toast_relname, sizeof(toast_relname),
             "pg_toast_%u", relOid);
    snprintf(toast_idxname, sizeof(toast_idxname),
             "pg_toast_%u_index", relOid);

    /* this is pretty painful...  need a tuple descriptor */
    tupdesc = CreateTemplateTupleDesc(3, false);
    TupleDescInitEntry(tupdesc, (AttrNumber) 1,
                       "chunk_id",
                       OIDOID,
                       -1, 0);
    TupleDescInitEntry(tupdesc, (AttrNumber) 2,
                       "chunk_seq",
                       INT4OID,
                       -1, 0);
    TupleDescInitEntry(tupdesc, (AttrNumber) 3,
                       "chunk_data",
                       BYTEAOID,
                       -1, 0);

    /*
     * Ensure that the toast table doesn't itself get toasted, or we'll be
     * toast :-(.  This is essential for chunk_data because type bytea is
     * toastable; hit the other two just to be sure.
     */
    tupdesc->attrs[0]->attstorage = 'p';
    tupdesc->attrs[1]->attstorage = 'p';
    tupdesc->attrs[2]->attstorage = 'p';

    /*
     * Toast tables for regular relations go in pg_toast; those for temp
     * relations go into the per-backend temp-toast-table namespace.
     */
    if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
        namespaceid = GetTempToastNamespace();
    else
        namespaceid = PG_TOAST_NAMESPACE;

    /*
     * Use binary-upgrade override for pg_type.oid, if supplied.  We might be
     * in the post-schema-restore phase where we are doing ALTER TABLE to
     * create TOAST tables that didn't exist in the old cluster.
     */
    if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_toast_pg_type_oid))
    {
        toast_typid = binary_upgrade_next_toast_pg_type_oid;
        binary_upgrade_next_toast_pg_type_oid = InvalidOid;
    }

    toast_relid = heap_create_with_catalog(toast_relname,
                                           namespaceid,
                                           rel->rd_rel->reltablespace,
                                           toastOid,
                                           toast_typid,
                                           InvalidOid,
                                           rel->rd_rel->relowner,
                                           tupdesc,
                                           NIL,
                                           RELKIND_TOASTVALUE,
                                           rel->rd_rel->relpersistence,
                                           shared_relation,
                                           mapped_relation,
                                           true,
                                           0,
                                           ONCOMMIT_NOOP,
                                           reloptions,
                                           false,
                                           true,
                                           true,
#ifdef _SHARDING_
                                           RelationHasExtent(rel),
#endif
                                           NULL);
    Assert(toast_relid != InvalidOid);

    /* make the toast relation visible, else heap_open will fail */
    CommandCounterIncrement();

    /* ShareLock is not really needed here, but take it anyway */
    toast_rel = heap_open(toast_relid, ShareLock);

    /*
     * Create unique index on chunk_id, chunk_seq.
     *
     * NOTE: the normal TOAST access routines could actually function with a
     * single-column index on chunk_id only. However, the slice access
     * routines use both columns for faster access to an individual chunk. In
     * addition, we want it to be unique as a check against the possibility of
     * duplicate TOAST chunk OIDs. The index might also be a little more
     * efficient this way, since btree isn't all that happy with large numbers
     * of equal keys.
     */

    indexInfo = makeNode(IndexInfo);
    indexInfo->ii_NumIndexAttrs = 2;
    indexInfo->ii_KeyAttrNumbers[0] = 1;
    indexInfo->ii_KeyAttrNumbers[1] = 2;
    indexInfo->ii_Expressions = NIL;
    indexInfo->ii_ExpressionsState = NIL;
    indexInfo->ii_Predicate = NIL;
    indexInfo->ii_PredicateState = NULL;
    indexInfo->ii_ExclusionOps = NULL;
    indexInfo->ii_ExclusionProcs = NULL;
    indexInfo->ii_ExclusionStrats = NULL;
    indexInfo->ii_Unique = true;
    indexInfo->ii_ReadyForInserts = true;
    indexInfo->ii_Concurrent = false;
    indexInfo->ii_BrokenHotChain = false;
	indexInfo->ii_Am = BTREE_AM_OID;
    indexInfo->ii_AmCache = NULL;
    indexInfo->ii_Context = CurrentMemoryContext;

    collationObjectId[0] = InvalidOid;
    collationObjectId[1] = InvalidOid;

    classObjectId[0] = OID_BTREE_OPS_OID;
    classObjectId[1] = INT4_BTREE_OPS_OID;

    coloptions[0] = 0;
    coloptions[1] = 0;

    index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
				 InvalidOid, InvalidOid,
                 indexInfo,
                 list_make2("chunk_id", "chunk_seq"),
                 BTREE_AM_OID,
                 rel->rd_rel->reltablespace,
                 collationObjectId, classObjectId, coloptions, (Datum) 0,
				 INDEX_CREATE_IS_PRIMARY, 0, true, true, NULL);

    heap_close(toast_rel, NoLock);

    /*
     * Store the toast table's OID in the parent relation's pg_class row
     */
    class_rel = heap_open(RelationRelationId, RowExclusiveLock);

    reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
    if (!HeapTupleIsValid(reltup))
        elog(ERROR, "cache lookup failed for relation %u", relOid);

    ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;

    if (!IsBootstrapProcessingMode())
    {
        /* normal case, use a transactional update */
        CatalogTupleUpdate(class_rel, &reltup->t_self, reltup);
    }
    else
    {
        /* While bootstrapping, we cannot UPDATE, so overwrite in-place */
        heap_inplace_update(class_rel, reltup);
    }

    heap_freetuple(reltup);

    heap_close(class_rel, RowExclusiveLock);

    /*
     * Register dependency from the toast table to the master, so that the
     * toast table will be deleted if the master is.  Skip this in bootstrap
     * mode.
     */
    if (!IsBootstrapProcessingMode())
    {
        baseobject.classId = RelationRelationId;
        baseobject.objectId = relOid;
        baseobject.objectSubId = 0;
        toastobject.classId = RelationRelationId;
        toastobject.objectId = toast_relid;
        toastobject.objectSubId = 0;

        recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
    }

    /*
     * Make changes visible
     */
    CommandCounterIncrement();

    return true;
}

/*
 * Check to see whether the table needs a TOAST table.  It does only if
 * (1) there are any toastable attributes, and (2) the maximum length
 * of a tuple could exceed TOAST_TUPLE_THRESHOLD.  (We don't want to
 * create a toast table for something like "f1 varchar(20)".)
 * No need to create a TOAST table for partitioned tables.
 */
static bool
needs_toast_table(Relation rel)
{
	int32		data_length = 0;
	bool		maxlength_unknown = false;
	bool		has_toastable_attrs = false;
	TupleDesc	tupdesc;
	Form_pg_attribute *att;
	int32		tuple_length;
	int			i;

	/* No TOAST for partitioned tables */
	if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
	{
		return false;
	}

	tupdesc = rel->rd_att;
	att = tupdesc->attrs;

	for (i = 0; i < tupdesc->natts; i++)
	{
		if (att[i]->attisdropped)
			continue;
		data_length = att_align_nominal(data_length, att[i]->attalign);
		if (att[i]->attlen > 0)
		{
			/* Fixed-length types are never toastable */
			data_length += att[i]->attlen;
		}
		else
		{
			int32		maxlen = type_maximum_size(att[i]->atttypid,
												   att[i]->atttypmod);

			if (maxlen < 0)
				maxlength_unknown = true;
			else
				data_length += maxlen;
			if (att[i]->attstorage != 'p')
				has_toastable_attrs = true;
		}
	}
	if (!has_toastable_attrs)
		return false;			/* nothing to toast? */
	if (maxlength_unknown)
		return true;			/* any unlimited-length attrs? */
	tuple_length = MAXALIGN(SizeofHeapTupleHeader +
							BITMAPLEN(tupdesc->natts)) +
		MAXALIGN(data_length);
	return (tuple_length > TOAST_TUPLE_THRESHOLD);
}
